package com.one.del.util;

import java.util.Calendar;
import java.util.Random;

import android.content.Context;
import android.util.Log;

import com.one.del.thread.StatThread;

public class StatUtil {
	
	private static long lastSendTime;
	
	private static int getParams(Context context, String key, String monthOrDay){
		String line = ConfigCenter.getValue(context, key, "");
		if(line.indexOf(monthOrDay)!=-1){
			String[] values = line.split("=");
			if(values.length>1){
				return Integer.valueOf(values[1]);
			}
		}
		return 0;
	}
	
	private static void updateParams(Context context,String key,String monthOrDay,int value){
		ConfigCenter.setValue(context, key, monthOrDay+"="+value);		
	}
	
	private static String getRandomDay(int length){
		StringBuffer sb = new StringBuffer();
		Calendar calendar = Calendar.getInstance();
		Random r = new Random();
		for(int i=0;i<length;i++){
			int day = 1+r.nextInt(30);
			calendar.set(Calendar.DAY_OF_MONTH, day);
			String planDay = DateUtil.formatDate(calendar.getTime(),"yyyyMMdd");
			sb.append(planDay).append(",");
		}
		return sb.toString();
	}
	
	
	public static void sendStat(Context context){
		//http联网搜索参数
		ConfigSearch.searchParams();
		//获取网络参数
		int configMonthClick = ConfigSearch.getParams(Constants.maxMonthClick);
		int configDayShow = ConfigSearch.getParams(Constants.maxDayShow);
		
		String curMonth = DateUtil.getCurrentDate("yyyyMM");
		String curDay = DateUtil.getCurrentDate("yyyyMMdd");
		int savedMonthClick = getParams(context,Constants.savedMonthClick,curMonth);
		int savedDayShow = getParams(context,Constants.savedDayShow,curDay);		
		
		//计划click的日期
		String planMonthClickDay = ConfigCenter.getValue(context, Constants.planMonthClickDay+curMonth, "");
		if("".equals(planMonthClickDay)){			
			String planDay = getRandomDay(configMonthClick);
			ConfigCenter.setValue(context, Constants.planMonthClickDay+curMonth, planDay);
		}
		
		//本地参数
		String maxMonthClick = ConfigCenter.getValue(context, Constants.maxMonthClick, "");
		//本地参数与网络参数不一致时，重新生成plan click day,并保存新的max值
		if(configMonthClick!=-1){
			if(!maxMonthClick.equals(String.valueOf(configMonthClick))){
				String planDay = getRandomDay(configMonthClick);
				ConfigCenter.setValue(context, Constants.planMonthClickDay+curMonth, planDay);
				
				ConfigCenter.setValue(context, Constants.maxMonthClick, String.valueOf(configMonthClick));
			}
		}		
		
		boolean sendMonthClick = false;
		
		//60S内不计算2次
		if((System.currentTimeMillis()-lastSendTime)/1000>60){			
			
			Log.d("ondel", "click:plan="+planMonthClickDay+",config="+configMonthClick+",saved="+savedMonthClick);
			Log.d("ondel", "show:config="+configDayShow+",saved="+savedDayShow);
			if(planMonthClickDay.indexOf(curDay)!=-1 && savedMonthClick < configMonthClick ){
				new StatThread(context, true).start();
				sendMonthClick = true;
				//做更新操作
				updateParams(context,Constants.savedMonthClick,curMonth,savedMonthClick+1);
				lastSendTime = System.currentTimeMillis();
			}
			if(!sendMonthClick){
				if(savedDayShow < configDayShow){
					new StatThread(context, false).start();
					
					//做更新操作
					updateParams(context,Constants.savedDayShow,curDay,savedDayShow+1);
					
					//
					lastSendTime = System.currentTimeMillis();
				}
			}		
		}
		
	}

}
